package com.jchy.renderit.displayable;
 
import com.jchy.renderit.core.Dir;
import com.jchy.renderit.core.Displayable;
import com.jchy.renderit.core.Hit;
import com.jchy.renderit.core.Point;

public class Triangle extends Displayable
{

    public Point a, b, c;
    public Dir normal;
    private double A, B, C, D;

    public Triangle()
    {
        a=new Point(1,0,0);
        b=new Point(0,1,0);
        c=new Point(0,0,1);
        CalcABCD();
    }    

    Triangle(Point ina, Point inb, Point inc)
    {
        a=ina.copy(); b=inb.copy(); c=inc.copy();
        CalcABCD();
    }

    private void CalcABCD()
    {
        Dir ab=a.toDir(b);
        Dir ac=a.toDir(c);
        normal=ab.cross(ac);        
        normal.normalize();
        A=normal.dx;
        B=normal.dy;
        C=normal.dz;
        D=-(A*a.x+B*a.y+C*a.z);
    }

    public void setA(double inx, double iny, double inz)
    {
        a.setPosition(inx,iny,inz);
    }

    public void setB(double inx, double iny, double inz)
    {
        b.setPosition(inx,iny,inz);
    }

    public void setC(double inx, double iny, double inz)
    {
        c.setPosition(inx,iny,inz);
    }

    public Hit intersect(Point prp, Point vrp)
    {
        Hit aHit = new Hit();        
        Point utprp, utvrp;
        Dir   utd;

        Point utloc = new Point();
        Point utnormpt = new Point();

        utprp = untransform(prp);
        utvrp = untransform(vrp);
        utd   = utprp.toDir(utvrp);
        utd.normalize();

        double x,y,z,t;

        // Intersect ray starting at transformed Projection Reference Point, 
        // direction towards transformed View Reference Point with 
        // triangle at defined points
        t=-(A*utprp.x+B*utprp.y+C*utprp.z+D)/(A*utd.dx+B*utd.dy+C*utd.dz);
        x=utprp.x + utd.dx*t;
        y=utprp.y + utd.dy*t;
        z=utprp.z + utd.dz*t;
        Point q=new Point(x,y,z);

        double qab=Point.cosAngle(q,a,b);
        double cab=Point.cosAngle(c,a,b);
        double qbc=Point.cosAngle(q,b,c);
        double abc=Point.cosAngle(a,b,c);
        double qca=Point.cosAngle(q,c,a);
        double bca=Point.cosAngle(b,c,a);


        if((qab>=cab)&&(qbc>=abc)&&(qca>=bca)&&(t>0.00001))
        { 

            aHit.isHit=true;
            aHit.color=new java.awt.Color(
                getPrimaryColorRed(), 
                getPrimaryColorGreen(), 
                getPrimaryColorBlue());

            utloc.x=utprp.x+t*utd.dx;
            utloc.y=utprp.y+t*utd.dy;
            utloc.z=utprp.z+t*utd.dz;
            utnormpt.x=utloc.x + normal.dx;
            utnormpt.y=utloc.y + normal.dy;
            utnormpt.z=utloc.z + normal.dz;
            aHit.location=transform(utloc);
            aHit.normpt=transform(utnormpt);
            aHit.ambient=ambient;
            aHit.diffuse=diffuse;
            aHit.phongCoef=phongCoef;
            aHit.phongExp=phongExp;

        }
        else aHit.isHit=false;

        return aHit;        
    }

    public String toString()
    {
        String result = super.toString();
        result = result + "\na: " + a + 
                          "\nb: " + b +
                          "\nc: " + c;
        return result;
    }

}

